<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class pro_kontrol_sertifikasi extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('pro_kontrol_sertifikasi_model', 'KontrolSertifikasi');
		//$this->load->model('isp_combo_model', 'Combo');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Setup kontrol sertifikasi";	
		//$data['arr_customer']	= $this->Combo->IspCustomer();		
		viewByTemplate('pro_kontrol_sertifikasi/index', $data, 'grid');
	}

	function id_auto(){
		$query1 = $this->db->query("SELECT id_sertifikasi FROM (SELECT * FROM pro_kontrol_sertifikasi ORDER BY id_sertifikasi DESC) WHERE rownum <=1");
	 				if ($query1->row_array()) {
	 					$getquery1 = $query1->row_array();
	 					$rowcount1 = $getquery1['id_sertifikasi'];
	 					$autonum1 = $rowcount1 + 1;
	 				} else {
					$autonum1 = 1;
				}
		$data['id_sertifikasi_auto'] = $autonum1;

		$query = $this->db->query("SELECT kode_alat FROM (SELECT * FROM pro_kontrol_sertifikasi ORDER BY kode_alat DESC) WHERE rownum <= 1");
		$getquery = $query->row_array();

		if($getquery)
			$MaxID = $getquery['kode_alat'];
		else
			$MaxID = "SP0000000";

		$NextID = (int) substr($MaxID,2,7);

		$NextID++;
		
		$data['kode_alat_auto'] = "SP" . sprintf("%07s",$NextID);

		echo json_encode($data);
	}

	function index2(){
		$data['title'] = "Setup kontrol sertifikasi";	
		//$data['arr_customer']	= $this->Combo->IspCustomer();		
		
		$query = $this->db->query("SELECT kode_alat FROM (SELECT * FROM pro_kontrol_sertifikasi ORDER BY kode_alat DESC) WHERE rownum <= 1");
		$getquery = $query->row_array();

		if($getquery)
			$MaxID = $getquery['kode_alat'];
		else
			$MaxID = "SP0000000";

		$NextID = (int) substr($MaxID,2,7);

		$NextID++;
		
		$data['kode_alat_auto'] = "SP" . sprintf("%07s",$NextID);


		viewByTemplate('pro_kontrol_sertifikasi/index', $data, 'grid');
	}

	
	
	function getdata()
	{
		$this->secure_session();
		echo $this->KontrolSertifikasi->GetJSON();
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");


		// Validation		
		if (post("kode_alat") == '') {
			$error->Add("kode_alat", "Kode alat", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM pro_kontrol_sertifikasi WHERE kode_alat = '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("kode_alat"))))."'")) > 0) {
			$error->Add("kode_alat", "Kode alat", $tpl_duplicate);
		}

		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data = array(
					'id_sertifikasi' => $this->input->post('id_sertifikasi'),
					'kode_alat' => $this->input->post('kode_alat'),
					'nama_peralatan' => $this->input->post('nama_peralatan'),
					'nomor_sertifikasi' => $this->input->post('nomor_sertifikasi'),
					'tgl_berlaku_awal' => $this->input->post('tgl_berlaku_awal'),
					'tgl_berlaku_akhir' => $this->input->post('tgl_berlaku_akhir'),
					);

					$data['tgl_berlaku_awal']	=	($data['tgl_berlaku_awal'] == '' ? NULL : date_format3($data['tgl_berlaku_awal'], 'YYYY-MM-DD'));
					$data['tgl_berlaku_akhir']	=	($data['tgl_berlaku_akhir'] == '' ? NULL : date_format3($data['tgl_berlaku_akhir'], 'YYYY-MM-DD'));

				if (post("mode") == "create")
				{
					$this->KontrolSertifikasi->Add($data);

					$ls_response = "Data Tersimpan";

				}
				else if (post("mode") == "edit")
				{
					
					$this->KontrolSertifikasi->Update($data, array('kode_alat'	=> $data['kode_alat']));

					$ls_response = "Data Tersimpan [edited]";
				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result		= "";
		$kode_alat 	= get('kode_alat');
		
		if ($kode_alat != "")
		{
			// Delete Single
			$arr_rule	= array('kode_alat' => $kode_alat);
			$ls_result = $this->KontrolSertifikasi->DeleteSingle($arr_rule);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('kode_alat' => $row['kode_alat']);
				$ls_result .= $this->KontrolSertifikasi->DeleteSingle($arr_rule);
			}
		}
		
		echo $ls_result;
	}


function cetak(){

		//Load PDF
		$this->load->helper('fpdf');

		//Load Data
		$data['data_list']= $this->KontrolSertifikasi->GetCetakList();

		//Load Printout
		$this->load->view('pro_kontrol_sertifikasi/cetak',$data);

	}
	
}
?>